1// todo============= 顶部Tab栏=========================
async function top_render() {
    const res = await axios.get('/index/index')
    // console.log(res);
    const newData = res.data.data.allCate
    // console.log(newData);
    document.querySelector('.container ul').innerHTML = newData.map((item) => {
        return `  
        <li class="menu-item menu-item-type-taxonomy menu-item-object-category ">
            <a title="" href="index.html"><span class="fa ${item.icon}"></span>${item.catename}</a>
        </li>
        `
    }).join('')
}
top_render()


// todo =============== 中间文章 ===========================
async function content_Render() {
    const res = await axios.get('index/show', {
        params: {
            id: 9
        }
    })
    console.log(res);
}
content_Render()


// todo ====================== 左边文章  ====================


// todo =============== 侧边图片 渲染 =====================
async function right_Render() {
    const res = await axios.get('/index/index')
    // console.log(res.data.data);
    const newDatatwo = res.data.data.sideBanner
    document.querySelector('.textwidget').innerHTML = newDatatwo.map((item) => {
        return ` 
         <a href="${item.advimglink}" rel="nofollow" target="_blank" title="${item.advimgdesc}">
            <img class="g430" style="width:360px;height:133px;" src="http://124.223.14.236:8060/${item.advimgsrc}" alt="阿里云开学季">
         </a>
       `
    }).join('')
}
right_Render()


//todo ================ 热门标签开始 渲染 =======================
async function render_Hot() {
    const res = await axios.get('/index/index')
    // console.log(res);
    // console.log(res.data.data.allTag);
    const newData = res.data.data.allTag
    // console.log(newData);
    document.querySelector('.widget_tags ul').innerHTML = newData.map((item) => {
        return ` 
             <li><a title="" href="">${item.tagname}</a></li>
            `
    }).join('')
}
render_Hot()


//todo ================ 友情链接开始 渲染 =======================
async function hot_right_render() {
    const res = await axios.get('/index/index')
    // console.log(res);
    // console.log(res.data.data.allLink);
    const newData = res.data.data.allLink
    // console.log(newData);
    document.querySelector('.widget_comments ul').innerHTML = newData.map((item) => {
        return ` 
            <li>
                <a href="${item.url}"><img src="http://124.223.14.236:8060/${item.img}" alt="">
                    <p>${item.des}</p>
                </a>
            </li >
        `
    }).join('')
}
hot_right_render()

//todo ================ 热门文章开始 渲染 =======================
async function article_render() {
    const res = await axios.get('/index/new')
    // console.log(res.data.data.list.data);
    const newData = res.data.data.list.data
    // console.log(newData);
    document.querySelector('.widget ul').innerHTML = newData.map((item) => {
        return ` 
        <li>
            <a target="_blank" href="">
                <span class="thumbnail">
                    <img src="http://124.223.14.236:8060/${item.pic}" class="thumb" />
                </span>
                <span class="text">${item.title}</span>
                <span class="text-muted post-views">阅读${item.click}</span>
            </a>
        </li>
        `
    }).join('')
}
article_render()

// //todo ================ 底部 友情链接  =======================
async function bottom_render() {
    const res = await axios.get('/index/index')
    // console.log(res);
    // console.log(res.data.data.allLink);
    const newData = res.data.data.allLink
    // console.log(newData);
    document.querySelector('.footer .link').innerHTML = newData.map((item) => {
        return ` 
        <a href="" title="obj.des">${item.des}</a> <span>|</span>
        `
    }).join('')
}
bottom_render()

// //todo ================ 底部 版权 ============================
async function copyright_render() {
    const res = await axios.get('/index/index')
    // console.log(res);
    const newData = res.data.data.config.allData
    // console.log(newData);
    newData.forEach((item) => {
        if (item.id === 55) {
            document.querySelector('.footer p').innerHTML = ` <p>&copy; 2021 <a href="http://blog.weknow.cn">优点达资讯,</a>
            &nbsp;&nbsp;联系邮箱：${item.attr_value}
            &nbsp;&nbsp; <a href="">备案号信息</a>
            </p>`
            // document.querySelector('.footer p').innerHTML = newData.map((item) => {
            //     return `
            // <p>&copy; 2021 <a href="http://blog.weknow.cn">优点达资讯,</a>
            // &nbsp;&nbsp;联系邮箱：${item.attr_value}
            // &nbsp;&nbsp; <a href="">备案号信息</a>
            // </p>
            //     `
            // }).join('')
        }
    });
}
copyright_render()

const id = JSON.parse(localStorage.getItem('textid'))
// console.log(id);
async function fn() {
    const { data: { data: { info, next, prev, recommend } } } = await axios.get('/index/show', {
        params: { id }
    })
    console.log(prev);
    document.querySelector('.textTitle').innerHTML = info.title
    document.querySelector('.textTitle1').innerHTML = info.title
    document.querySelector('.catename').innerHTML = info.catename
    document.querySelector('.click').innerHTML = `阅读(${info.click})`
    document.querySelector('.lovenum').innerHTML = info.lovenum
    document.querySelector('[rel="tag"]').innerHTML = info.tags
    document.querySelector('.article-content').innerHTML = `${info.content}`
    document.querySelector('center img').src = `http://124.223.14.236:8060/${info.pic}`
    document.querySelector('[rel="prev"]').innerHTML = prev[0].title
    document.querySelector('[rel="next"]').innerHTML = next[0].title
    console.log(recommend[0].pic);
    document.querySelector('#tuijian').innerHTML = recommend.map(({ title, pic }) => `
    <li>
        <a target="_blank" href="">
        <span>
            <img src="http://124.223.14.236:8060/${pic}" class="thumb" />
        </span>
        ${title}
        </a>
    </li>
    `).join('')
}
fn()
// todo ===================== 顶部 登入注册功能 ==============================
async function getuser() {
    const { data: { data } } = await axios.get('user/getUserInfo', {
        headers: { token: localStorage.getItem('token') }
    })
    // console.log(data);
    // 账号信息
    document.querySelector('.slinks').innerHTML = `
     <i class="iconfont icon-yonghu"></i>你好${data.userInfo.username} 
    <i class="iconfont icon-exit-full"></i>
    `
}
getuser()

//todo ============== 是否点赞 ==========================
// 是否点赞
let flag = true
function active(add) {
    axios.get('/user/userDataHandle',
        {
            headers: { 'token': localStorage.getItem('token') },

            params: {
                type: 2, //1 表收藏  2 表点赞
                action: add,  // add 表点赞或收藏    del 表取消点赞 或者收藏
                article_id: id
            }
        }).then(res => {
            //    alert('点赞成功')
        })
}
document.querySelector('.article-social').addEventListener('click', function (e) {
    // console.log(2);
    if (flag) {

        document.querySelector('#click').innerHTML++
        document.querySelector('.action').classList.add('action-like')
        layer.msg('点赞成功')
        active("add")
        flag = false

    }
    //  else {

    //     document.querySelector('#click').innerHTML--
    //     // document.querySelector('.action').style.backgroundColor = '#eee'
    //     document.querySelector('.action').classList.remove('action-like')
    //     active("del")
    //     layer.msg('取消点赞')
    //     flag = true
    // }
})
// todo===============是否收藏===========================、
// 是否收藏
let flags = true
function actives(add) {
    axios.get('/user/userDataHandle',
        {
            headers: { 'token': localStorage.getItem('token') },

            params: {
                type: 2,  //1 表收藏  2 表点赞
                action: add, // add 表点赞或收藏    del 表取消点赞 或者收藏
                article_id: id
            }
        }).then(res => {
            //    alert('点赞成功')
        })
}
document.querySelector('.article-header').addEventListener('click', function (e) {
    if (e.target.classList.contains('isCollect')) {
        // console.log(1)\

        if (flags) {
            document.querySelector('.isCollect').innerHTML = `已收藏`
            layer.msg('收藏成功')
            actives("add")
            flags = false
        } else {
            document.querySelector('.isCollect').innerHTML = `点击收藏`
            layer.msg('取消收藏')
            actives("del")
            flags = true
        }
    }
})
